package com.syg.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.syg.model.ExcelModel01;
import lombok.SneakyThrows;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

public class ExcelModel01Listener implements ReadListener<ExcelModel01> {

    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 2000;

    int readRows = 0;

    /**
     * 缓存的数据
     */
    private List<ExcelModel01> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);


    @SneakyThrows
    @Override
    public void invoke(ExcelModel01 data, AnalysisContext context) {

        if (data.getQuestionType() == null || "".equals(data.getQuestionType()) || "".equals(data.getQuestionType().replace(",", ""))) {
            // System.out.println("问题类型为空" + data.getId());
            return;
        }



        // 在文件夹目录下新建文件
        File file = new File("D:\\Cache\\Temp\\qc_history.sql");

        FileOutputStream fos = null;
        OutputStreamWriter osw = null;

        if (!file.exists()) {
            boolean hasFile = false;
            try {
                hasFile = file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (hasFile) {
                System.out.println("创建文件");
            }
            fos = new FileOutputStream(file);
        } else {
            fos = new FileOutputStream(file, true);
        }

        osw = new OutputStreamWriter(fos, "utf-8");

        String[] split = data.getQuestionType().split(",");
        for (String item : split) {
            if ("".equals(item) || item == null) {
                // System.out.println("问题类型分隔以后为空" + item);
                continue;
            }

            if (",Q01,Q02,Q03,Q04,Q05,Q020,".contains("," + item + ",")) {
                String parentSql = "insert into T_OCS_QUESTION_RESULT_LEVEL(ID,BIZ_ID,TYPE_LEVEL,PARENT_LEVEL,TYPE_LEVEL_ID,CREATE_TIME,CREATE_USER,UPDATE_TIME,UPDATE_USER,DEL_FLAG) values(dbms_random.string('A','31'),'" + data.getId() + "','E_ID_CARD_IMAGE_QUES','" + data.getId() + "',dbms_random.string('A','31'),to_date('2022-08-03','yyyy-mm-dd'),'admin',to_date('2022-08-03','yyyy-mm-dd'),'admin','0');";
                String subSql = "insert into T_OCS_QUESTION_RESULT_LEVEL(ID,BIZ_ID,TYPE_LEVEL,PARENT_LEVEL,TYPE_LEVEL_ID,CREATE_TIME,CREATE_USER,UPDATE_TIME,UPDATE_USER,DEL_FLAG) values(dbms_random.string('A','31'),'" + data.getId() + "','" + item + "','E_ID_CARD_IMAGE_QUES',dbms_random.string('A','31'),to_date('2022-08-03','yyyy-mm-dd'),'admin',to_date('2022-08-03','yyyy-mm-dd'),'admin','0');";

                // System.out.println(parentSql);
                // System.out.println(subSql);
                // 写入内容
                osw.write(parentSql);
                osw.write("\r\n");
                osw.write(subSql);
                // 换行
                osw.write("\r\n");
                continue;
            }

            if (",Q014,".contains("," + item + ",")) {
                String parentSql = "insert into T_OCS_QUESTION_RESULT_LEVEL(ID,BIZ_ID,TYPE_LEVEL,PARENT_LEVEL,TYPE_LEVEL_ID,CREATE_TIME,CREATE_USER,UPDATE_TIME,UPDATE_USER,DEL_FLAG) values(dbms_random.string('A','31'),'" + data.getId() + "','E_LIVING_IMAGE_QUES','" + data.getId() + "',dbms_random.string('A','31'),to_date('2022-08-03','yyyy-mm-dd'),'admin',to_date('2022-08-03','yyyy-mm-dd'),'admin','0');";
                String subSql = "insert into T_OCS_QUESTION_RESULT_LEVEL(ID,BIZ_ID,TYPE_LEVEL,PARENT_LEVEL,TYPE_LEVEL_ID,CREATE_TIME,CREATE_USER,UPDATE_TIME,UPDATE_USER,DEL_FLAG) values(dbms_random.string('A','31'),'" + data.getId() + "','" + item + "','E_LIVING_IMAGE_QUES',dbms_random.string('A','31'),to_date('2022-08-03','yyyy-mm-dd'),'admin',to_date('2022-08-03','yyyy-mm-dd'),'admin','0');";

                // System.out.println(parentSql);
                // System.out.println(subSql);

                osw.write(parentSql);
                osw.write("\r\n");
                osw.write(subSql);
                // 换行
                osw.write("\r\n");
                continue;
            }
            if (",Q06,Q07,".contains("," + item + ",")) {
                String parentSql = "insert into T_OCS_QUESTION_RESULT_LEVEL(ID,BIZ_ID,TYPE_LEVEL,PARENT_LEVEL,TYPE_LEVEL_ID,CREATE_TIME,CREATE_USER,UPDATE_TIME,UPDATE_USER,DEL_FLAG) values(dbms_random.string('A','31'),'" + data.getId() + "','E_FIELD_QUES','" + data.getId() + "',dbms_random.string('A','31'),to_date('2022-08-03','yyyy-mm-dd'),'admin',to_date('2022-08-03','yyyy-mm-dd'),'admin','0');";
                String subSql = "insert into T_OCS_QUESTION_RESULT_LEVEL(ID,BIZ_ID,TYPE_LEVEL,PARENT_LEVEL,TYPE_LEVEL_ID,CREATE_TIME,CREATE_USER,UPDATE_TIME,UPDATE_USER,DEL_FLAG) values(dbms_random.string('A','31'),'" + data.getId() + "','" + item + "','E_FIELD_QUES',dbms_random.string('A','31'),to_date('2022-08-03','yyyy-mm-dd'),'admin',to_date('2022-08-03','yyyy-mm-dd'),'admin','0');";

                // System.out.println(parentSql);
                // System.out.println(subSql);
                osw.write(parentSql);
                osw.write("\r\n");
                osw.write(subSql);
                // 换行
                osw.write("\r\n");
                continue;
            }
            if (",Q09,Q010,Q011,Q015,".contains("," + item + ",")) {
                String parentSql = "insert into T_OCS_QUESTION_RESULT_LEVEL(ID,BIZ_ID,TYPE_LEVEL,PARENT_LEVEL,TYPE_LEVEL_ID,CREATE_TIME,CREATE_USER,UPDATE_TIME,UPDATE_USER,DEL_FLAG) values(dbms_random.string('A','31'),'" + data.getId() + "','E_PROTOCOL_QUES','" + data.getId() + "',dbms_random.string('A','31'),to_date('2022-08-03','yyyy-mm-dd'),'admin',to_date('2022-08-03','yyyy-mm-dd'),'admin','0');";
                String subSql = "insert into T_OCS_QUESTION_RESULT_LEVEL(ID,BIZ_ID,TYPE_LEVEL,PARENT_LEVEL,TYPE_LEVEL_ID,CREATE_TIME,CREATE_USER,UPDATE_TIME,UPDATE_USER,DEL_FLAG) values(dbms_random.string('A','31'),'" + data.getId() + "','" + item + "','E_PROTOCOL_QUES',dbms_random.string('A','31'),to_date('2022-08-03','yyyy-mm-dd'),'admin',to_date('2022-08-03','yyyy-mm-dd'),'admin','0');";

                // System.out.println(parentSql);
                // System.out.println(subSql);
                osw.write(parentSql);
                osw.write("\r\n");
                osw.write(subSql);
                // 换行
                osw.write("\r\n");
                continue;
            }
            if ("Q013".equals(item)) {
                String parentSql = "insert into T_OCS_QUESTION_RESULT_LEVEL(ID,BIZ_ID,TYPE_LEVEL,PARENT_LEVEL,TYPE_LEVEL_ID,CREATE_TIME,CREATE_USER,UPDATE_TIME,UPDATE_USER,DEL_FLAG) values(dbms_random.string('A','31'),'" + data.getId() + "','"+ item +"','" + data.getId() + "',dbms_random.string('A','31'),to_date('2022-08-03','yyyy-mm-dd'),'admin',to_date('2022-08-03','yyyy-mm-dd'),'admin','0');";

                // System.out.println(parentSql);
                osw.write(parentSql);
                // 换行
                osw.write("\r\n");
                continue;
            }

            if ("Q08".equals(item)) {
                String parentSql = "insert into T_OCS_QUESTION_RESULT_LEVEL(ID,BIZ_ID,TYPE_LEVEL,PARENT_LEVEL,TYPE_LEVEL_ID,CREATE_TIME,CREATE_USER,UPDATE_TIME,UPDATE_USER,DEL_FLAG) values(dbms_random.string('A','31'),'" + data.getId() + "','"+ item +"','" + data.getId() + "',dbms_random.string('A','31'),to_date('2022-08-03','yyyy-mm-dd'),'admin',to_date('2022-08-03','yyyy-mm-dd'),'admin','0');";

                // System.out.println(parentSql);
                osw.write(parentSql);
                // 换行
                osw.write("\r\n");
                continue;
            }

            if ("Q012".equals(item)) {
                String parentSql = "insert into T_OCS_QUESTION_RESULT_LEVEL(ID,BIZ_ID,TYPE_LEVEL,PARENT_LEVEL,TYPE_LEVEL_ID,CREATE_TIME,CREATE_USER,UPDATE_TIME,UPDATE_USER,DEL_FLAG) values(dbms_random.string('A','31'),'" + data.getId() + "','"+ item +"','" + data.getId() + "',dbms_random.string('A','31'),to_date('2022-08-03','yyyy-mm-dd'),'admin',to_date('2022-08-03','yyyy-mm-dd'),'admin','0');";

                // System.out.println(parentSql);
                osw.write(parentSql);
                // 换行
                osw.write("\r\n");
                continue;
            }
        }
        osw.flush();


    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        System.out.println("处理完毕");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
    }
}
